##*****************************************************************************
## FIGURE A1
##*****************************************************************************
## 
## R Version: R version 3.2.4 (2016-03-1)
##

##LOADING PACKAGES
library(ggplot2) ##Version: 2.1.0

##SET WORKING DIRECTORY
setwd('/workspace/fed_courts/scaling_paper/')

##LOADING DATASET
petition <- read.csv('replication_materials/data/cert_petition_data_matched_to_dime.csv')

##REMOVING ISSUE AREAS WITH FEWER THAN 20 CASES
tt <- table(petition$issueAreaName)
petition$issueAreaName2 <- as.character(petition$issueAreaName)
petition$issueAreaName2[!(petition$issueAreaName %in% names(tt[tt>=20]))] <- NA
petition$issueAreaName2 <- factor(petition$issueAreaName2)

 
d1 <- with(petition,aggregate((cfscore_pet-cfscore_resp),list(issueAreaName2),mean,na.rm=T))
d2 <- with(petition[!is.na(petition$cfscore_pet-petition$cfscore_resp),] ,aggregate(ddirect,list(issueAreaName2),mean,na.rm=T))

dd <- cbind(d1,d2[match(d1[,1],d2[,1]),2])
q <- qplot(x=dd[,2],y=dd[,3])
q <- q + geom_text(aes(x=dd[,2],y=dd[,3] +.01,label=dd[,1]))
q <- q + xlab('Average Difference in DIME scores (Petitioner Atty - Respondent Atty)')
q <- q + ylab('Proportion of Cases Decisions for Petitioner Coded as Conservative')
q <- q + xlim(min(dd[,2])-.1,max(dd[,2])+0.1)
q <- q +  theme_bw()
q <- q + theme(axis.title= element_text(size=14))


##PLOTTING PDF
pdf(file='replication_materials/figures/figure_A1_issueareas.pdf',width = 10,height= 10)
print(q)
dev.off()

##PLOTTING EPS
setEPS()
postscript(file='replication_materials/figures/figure_A1_issueareas.eps',width = 10,height= 10)
print(q)
dev.off()
